<?php
class ReviewsController extends AppController {

	var $name = 'Reviews';

	function index() {
		$this->Review->recursive = 0;
		$this->set('reviews', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid review', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('review', $this->Review->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Review->create();
			if ($this->Review->save($this->data)) {
				$this->Session->setFlash(__('The review has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The review could not be saved. Please, try again.', true));
			}
		}
		$rsvps = $this->Review->Rsvp->find('list');
		$this->set(compact('rsvps'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid review', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Review->save($this->data)) {
				$this->Session->setFlash(__('The review has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The review could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Review->read(null, $id);
		}
		$rsvps = $this->Review->Rsvp->find('list');
		$this->set(compact('rsvps'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for review', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Review->delete($id)) {
			$this->Session->setFlash(__('Review deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Review was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        function test(){
            
        }

        function review_action($id_event = null, $id_rsvp = null, $id_review = null) {

            if (!$id_event) {
                $this->Session->setFlash(__('Invalid id for event', true));
		$this->redirect(array('controller' => 'events','action'=>'index'));
            }elseif (!$id_rsvp){
                $this->Session->setFlash(__('Invalid id for rsvp', true));
		$this->redirect(array('controller' => 'events','action'=>'index'));
            }

            $date = date('Y-m-d H:i:s');

            if ($id_review){//update

                $this->Review->id = $id_review;

                $date = date('Y-m-d H:i:s');

                $this->Review->saveField('review_ts', $date);
                $this->Review->saveField('comment', $this->data['Event']['comment']);
                $this->Review->saveField('rating', $this->data['Event']['rating']);

                $this->redirect(array('controller' => 'events','action'=>'view','rsvps',$id_event));

            }else{//new review

                $this->data['Review']['rsvp_id'] = $id_rsvp;
                $this->data['Review']['review_ts'] = $date;
                $this->data['Review']['comment'] = $this->data['Event']['comment'];
                $this->data['Review']['rating'] = $this->data['Event']['rating'];

                if ($this->Review->save($this->data)){
                    $this->Session->setFlash(__('The review has been saved', true));
                }else{
                    $this->Session->setFlash(__('The review could not be saved. Please try again.', true));
                }

                $this->redirect(array('controller' => 'events','action'=>'view','rsvps',$id_event));

            }


        }
}
?>